package com.itqf.controller;

import com.itqf.entity.Address;
import com.itqf.entity.Car;
import com.itqf.entity.Order;
import com.itqf.entity.User;
import com.itqf.service.AddressService;
import com.itqf.service.CarService;
import com.itqf.service.OrderService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.service.impl.CarServiceImpl;
import com.itqf.service.impl.OrderServiceImpl;
import com.itqf.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/orders")
public class OrderController extends BaseServlet{

    public String preView(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {
        String userno = req.getParameter("userno");
        AddressService addressService = new AddressServiceImpl();
        System.out.println(userno);
        List<Address> addressList = addressService.findAllAddressByUserno(Integer.parseInt(userno));
        CarService carService = new CarServiceImpl();
        List<Car> allCar = carService.findAll(Integer.parseInt(userno));
        req.setAttribute("addressList",addressList);
        req.setAttribute("carList",allCar);
        return Constants.FOW+"/order.jsp";
    }
    public String create(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {
        String userno = req.getParameter("userno");
        String aid = req.getParameter("aid");
        String sum = req.getParameter("sum");
        OrderService orderService = new OrderServiceImpl();
        orderService.createOrder(aid,userno,sum);

       return Constants.FOW+"/orders?method=show";
    }
    public String show(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session =req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if(user==null){
            session.setAttribute("msg","请先登录！");
            return Constants.REDIRECT+"/login.jsp";
        }
        OrderService orderService = new OrderServiceImpl();
        List<Order> orderList =  orderService.findOrders(user.getUserno());
        req.setAttribute("orderList",orderList);
        return Constants.FOW+"/orderList.jsp";
    }
    public String detail(HttpServletRequest req, HttpServletResponse resp){

        String oid = req.getParameter("oid");
        OrderService orderService = new OrderServiceImpl();
        Order order =  orderService.findOrderByOid(oid);
        req.setAttribute("order",order);
        return Constants.FOW+"/orderDetail.jsp";
    }
}
